package com.supreme.commonboot.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

//@Component
public class SupremeAwareUtils implements BeanFactoryAware, ApplicationContextAware {

    public static DefaultListableBeanFactory beanFactory;
    public static AnnotationConfigServletWebServerApplicationContext applicationContext;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        SupremeAwareUtils.beanFactory = (DefaultListableBeanFactory) beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SupremeAwareUtils.applicationContext = (AnnotationConfigServletWebServerApplicationContext) applicationContext;
    }

    public static <T> T getBeanFactory(Class<T> clazz) {
        return beanFactory.getBean(clazz);
    }

    public static Object getBeanFactory(String beanName) {
        return beanFactory.getBean(beanName);
    }
}
